var AV = require('leanengine');
var redis = require('redis'),
    client = redis.createClient(process.env['REDIS_URL_AREarth']);
client.on('error', function (err) {
    return console.error('redis err: %s', err);
});
client.on('ready', function (err) {
    console.log('ready');
});

var count = 0;
/**
 * 一个简单的云代码方法
 */
AV.Cloud.define('hello', function (request, response) {
    response.success('Hello world!');
});

//用户进入等待
AV.Cloud.define('enterQueue', function (request, response) {
    var activityID = request.params.activityID; //活动id
    var playerID = request.params.playerID;  //用户id
    var playerType = request.params.playerType;  //用户类型 normal 和 vip,假定是从客户端传回

    //普通用户插入链表的右边，等候
    if (playerType === 'normal') {
        client.lrem(activityID, 0, playerID, function () {
            client.rpush(activityID, playerID, function () {
                response.success('OK');
            });
        });
    }
    //vip用户插入vip链表，等候
    else if (playerType == 'vip') {
        client.lrem(activityID + 'vip', 0, playerID, function () {
            client.rpush(activityID + 'vip', playerID, function () {
                response.success('OK');
            });
        });
    }
});

//用户从等候进入游戏
AV.Cloud.define('enterGame', function (request, response) {
    var activityID = request.params.activityID; //活动id
    if (count == 10) {
        client.lpop(activityID + 'vip', function (err, resp) {
            //vip列已空
            if (!!resp){
                client.lpop(activityID, function (err, resp) {
                    response.success(resp);
                    count++;
                    return;
                });
            }
            response.success(resp);
        });
        count = 0;
    } else {
        client.lpop(activityID, function (err, resp) {
            //普通列已空
            if (!!resp){
                client.lpop(activityID + "vip", function (err, resp) {
                    response.success(resp);
                    count = 0;
                    return;
                });
            }
            response.success(resp);
        });
        count++;
    }
});

//用户退出等待
AV.Cloud.define('exitQueue', function (request, response) {
    var activityID = request.params.activityID; //活动id
    var playerID = request.params.playerID;  //用户id
    var playerType = request.params.playerType;  //用户类型 normal 和 vip,假定是从客户端传回

    //从普通用户list中删除所有playerID相等的元素
    if (playerType === 'normal') {
        client.lrem(activityID, 0, playerID, function () {
            response.success('OK');
        });
    }
    //从vip用户list中删除
    else if (playerType == 'vip') {
        client.lrem(activityID + 'vip', 0, playerID, function () {
            response.success('OK');
        });
    }
});

AV.Cloud.define('queryMember', function (request, response) {
    var activityID = request.params.activityID; //活动id
    var playerType = request.params.playerType;  //用户类型 normal 和 vip,假定是从客户端传回
    //从普通用户list中删除所有playerID相等的元素
    if (playerType === 'normal') {
        client.lrange(activityID, 0, -1, function (err, resp) {
            response.success(resp);
        });
    }
    //从vip用户list中删除
    else if (playerType == 'vip') {
        client.lrange(activityID + 'vip', 0, -1, function (err, resp) {
            response.success(resp);
        });
    }
});


//排队
function redisLineKey(arplayID, round) {
    return arplayID + '_' + round + "_line";
}


function queryLineIndex(arplayID, round, playerID) {
    client.zrank(redisLineKey(arplayID, round), playerID, function (err, reply) {
        if (err) {
            return err;
        } else {
            return reply + 1;
        }
    });
}

//加入排队，返回排队位置
AV.Cloud.define('addinLine',function (request, response) {
	var arplayID = request.params.arplayID;
	var round = request.params.round;
	var playerID = request.params.playerID;
    client.zadd(redisLineKey(arplayID, round), Date.now(), playerID, function (err, reply) {
        if (err) {
            response.error(err);
        } else {
            client.zrank(redisLineKey(arplayID, round), playerID, function (err2, reply) {
                if (err2) {
                    response.error(err2);
                } else {
                    response.success(reply +1);
                }
            });
        }
    });
});

//查询排队位置
AV.Cloud.define('queryLineIndex',function (request, response) {
	var arplayID = request.params.arplayID;
	var round = request.params.round;
	var playerID = request.params.playerID;
    client.zrank(redisLineKey(arplayID, round), playerID, function (err, reply) {
        if (err) {
            response.error(err);
        } else {
            response.success(reply + 1);
        }
    });
});
//退出排队
AV.Cloud.define('exitLine',function (request, response){
	var arplayID = request.params.arplayID;
	var round = request.params.round;
	var playerID = request.params.playerID;
    client.zrem(redisLineKey(arplayID, round),playerID,function(err,reply){
        if(err){
            response.error(err);
        }
        else{
        	response.success("退出成功");
        }
    });
});

module.exports = AV.Cloud;
